package com.canbot.u05.activity.customvoice.tool

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.Point
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View

import com.canbot.u05.R
import com.canbot.u05.utils.ToastTools
import kotlinx.android.synthetic.main.custom_dialog_text_to_speech_view.*

/**
 * CustomDialogTextToSpeechView
 *
 * @author Liu.Heng
 * @date 2019/6/3
 */
class CustomDialogTextToSpeechView(private val mContext: Context, private val mString: String, private val listener: OnCloseListener?, private val position: Int) : Dialog(mContext) {

    private val watcher = object : TextWatcher {

        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            val size = 500
            if (s.length > size) {
                my_editText.setText(s.subSequence(0, size))
                my_editText.setSelection(size)
                ToastTools.newInstance(mContext.applicationContext).showText(mContext.getString(R.string.input_up_words2))
            }
        }

        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int,
                                       after: Int) {

        }

        override fun afterTextChanged(s: Editable) {

        }
    }

    interface OnCloseListener {

        /***
         * 点击按钮回调
         * @param dialog  mContext
         * @param confirm true or false
         * @param string mEditText内容
         */
        fun callBack(dialog: Dialog, confirm: Boolean, string: String)

        /***
         * 点击按钮回调
         * @param dialog  mContext
         * @param position 下标
         * @param string mEditText内容
         */
        fun callBackPosition(dialog: Dialog, position: Int, string: String)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.custom_dialog_text_to_speech_view)
        setCanceledOnTouchOutside(false)
        /**设置弹窗的宽度*/
        val m = window?.windowManager
        val d = m?.defaultDisplay
        val p = window?.attributes
        val size = Point()
        d?.getSize(size)
        p?.width = size.x
        p?.height = size.y
        window?.attributes = p
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        hideBottomUIMenu()
        initView()
    }

    private fun initView() {

        cancel_btn.setOnClickListener(::onClick)
        affirm_btn.setOnClickListener(::onClick)
        if (mString.isNotEmpty()) {
            my_editText.setText(mString)
        }
        my_editText.addTextChangedListener(watcher)
        my_editText.setOnFocusChangeListener { _, _ -> my_editText.setSelection(my_editText.length()) }
    }

    private fun onClick(view: View) {
        when (view.id) {
            R.id.cancel_btn -> this.dismiss()
            R.id.affirm_btn -> {
                if (mString.isEmpty()) {
                    listener?.callBack(this, true, my_editText.text.toString().trim { it <= ' ' })
                } else {
                    listener?.callBackPosition(this, position, my_editText.text.toString().trim { it <= ' ' })
                }
            }
            else -> {
            }
        }
    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    private fun hideBottomUIMenu() {
        val attributes = window?.attributes
        attributes?.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        window?.attributes = attributes
        window?.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        window?.decorView?.setOnSystemUiVisibilityChangeListener { _ ->
            var uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    /**布局位于状态栏下方*/
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                    /**全屏*/
                    View.SYSTEM_UI_FLAG_FULLSCREEN or
                    /**隐藏导航栏*/
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            uiOptions = uiOptions or 0x00001000
            window?.decorView?.systemUiVisibility = uiOptions

        }
    }
}